package cn.agiledata.bank.linkbank.impl.chinapay;

import java.math.BigDecimal;
import java.util.Date;

import cn.agiledata.bank.common.util.DateUtil;
import cn.agiledata.bank.linkbank.exception.LinkBankException;
import cn.agiledata.bank.linkbank.impl.AbstractBankNetPayRequest;
import cn.agiledata.bank.linkbank.impl.AbstractBankPayRequest;
import cn.agiledata.bank.linkbank.logic.DigitalSignGeneratable;

/**
 * 中国银联电子网上支付请求
 * 
 * @author 马贤波
 * 
 */
public final class ChinapayNetPayRequest extends AbstractBankNetPayRequest
		implements DigitalSignGeneratable {
	private static final BigDecimal HUNDRED = new BigDecimal(100);

	private ChinapayOrderIdTransformer orderIdTransformer;

	private String privateKeyFilePath;

	private String key;

	private String retUrl;
	

	public ChinapayNetPayRequest() {
		super();
		this.orderIdTransformer = new ChinapayOrderIdTransformer();
		init();
	}

	public void setPrivateKeyFilePath(String privateKeyFilePath) {
		this.privateKeyFilePath = privateKeyFilePath;
	}

	private void init() {
		setParameter(ChinapayNetConstant.VERSION, "20040916"); // 版本号
		setParameter(ChinapayNetConstant.REQUEST_CURRENCY_TYPE,
				ChinapayNetConstant.CURRENCY_TYPE_RMB); // 货币种类：人民币
		setParameter(ChinapayNetConstant.REQUEST_TRANS_TYPE,
				ChinapayNetConstant.TRANS_TYPE_PAY); // 交易类型：消费
	}

	protected AbstractBankPayRequest getCopiedInstance() {
		ChinapayNetPayRequest request = new ChinapayNetPayRequest();
		request.orderIdTransformer = this.orderIdTransformer;
		request.privateKeyFilePath = this.privateKeyFilePath;
		return request;
	}

	public String getMerchantUrl() {
		return getParameter(ChinapayNetConstant.MERCHANT_URL_PAGE);
	}

	public void setMerchantUrl(String merchantUrl) {
		setParameter(ChinapayNetConstant.MERCHANT_URL_PAGE, merchantUrl);
		setParameter(ChinapayNetConstant.MERCHANT_URL_BACKGROUND, merchantUrl);
	}

	public void setMerchantId(String merchantId) {
		setParameter(ChinapayNetConstant.REQUEST_MERCHANT_ID, merchantId);
		this.orderIdTransformer.setMerchantId(merchantId);
	}

	/**
	 * 
	 * @param bankGateId
	 *            银行网关编号
	 */
	public void setBankGateId(String bankGateId) {
		setParameter(ChinapayNetConstant.BANK_GATE_ID, bankGateId);
	}

	public void setAmount(BigDecimal amount) {
		amount = amount.multiply(HUNDRED).setScale(0);
		String amountValue = amount.toString();
		while (amountValue.length() < 12) {
			amountValue = "0" + amountValue;
		}
		setParameter(ChinapayNetConstant.REQUEST_AMOUNT, amountValue);
	}

	public void setDate(Date date) {
		String dateValue = DateUtil.date2String(date,
				ChinapayNetConstant.DATE_FORMAT);
		setParameter(ChinapayNetConstant.REQUEST_DATE, dateValue);
	}

	public void setOrderId(String orderId) {
		orderId = this.orderIdTransformer.transId2OrderId(orderId);
		setParameter(ChinapayNetConstant.REQUEST_ORDER_ID, orderId);
	}

	public void setRemark(String remark) {
		setParameter(ChinapayNetConstant.REMARK, remark);
	}

	private void setDigitalSign(String digitalSign) {
		setParameter(ChinapayNetConstant.REQUEST_DIGITAL_SIGN, digitalSign);
	}

	public String generateDigitalSign(String cryptoKey)
			throws LinkBankException {
		String digitalSign = ChinapayUtils.generateDigitalSign(getParameters(),
				this.privateKeyFilePath);
		setDigitalSign(digitalSign);
		return digitalSign;
	}

	public String getCryptoKeyDescription() {
		return null;
	}

	public boolean validateCryptoKey(String cryptoKey) {
		return true;
	}

	public String getKey() {
		return key;
	}

	public void setKey(String key) {
		setParameter(ChinapayNetConstant.KEY,key);
	}

	public String getRetUrl() {
		return retUrl;
	}

	public void setRetUrl(String retUrl) {
		setParameter(ChinapayNetConstant.MERCHANT_URL_PAGE,retUrl);
	}

}
